home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C25 / TrashVisitor.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  2.8 KB  |  114 lines

  1. //: C25:TrashVisitor.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. //{L} VisitorTrashPrototypeInit
  7. //{L} fillBin Trash TrashStatics 
  8. // The "visitor" pattern
  9. #include "Visitor.h"
  10. #include "fillBin.h"
  11. #include "../purge.h"
  12. #include <iostream>
  13. #include <fstream>
  14. using namespace std;
  15. ofstream out("TrashVisitor.out");
  16.  
  17. // Specific group of algorithms packaged
  18. // in each implementation of Visitor:
  19. class PriceVisitor : public Visitor {
  20.   double alSum; // Aluminum
  21.   double pSum; // Paper
  22.   double gSum; // Glass
  23.   double cSum; // Cardboard
  24. public:
  25.   void visit(Aluminum* al) {
  26.     double v = al->weight() * al->value();
  27.     out << "value of Aluminum= " << v << endl;
  28.     alSum += v;
  29.   }
  30.   void visit(Paper* p) {
  31.     double v = p->weight() * p->value();
  32.     out << 
  33.       "value of Paper= " << v << endl;
  34.     pSum += v;
  35.   }
  36.   void visit(Glass* g) {
  37.     double v = g->weight() * g->value();
  38.     out << 
  39.       "value of Glass= " << v << endl;
  40.     gSum += v;
  41.   }
  42.   void visit(Cardboard* c) {
  43.     double v = c->weight() * c->value();
  44.     out << 
  45.       "value of Cardboard = " << v << endl;
  46.     cSum += v;
  47.   }
  48.   void total(ostream& os) {
  49.     os <<
  50.       "Total Aluminum: $" << alSum << "\n" <<
  51.       "Total Paper: $" << pSum << "\n" <<
  52.       "Total Glass: $" << gSum << "\n" <<
  53.       "Total Cardboard: $" << cSum << endl;
  54.   }
  55. };
  56.  
  57. class WeightVisitor : public Visitor {
  58.   double alSum; // Aluminum
  59.   double pSum; // Paper
  60.   double gSum; // Glass
  61.   double cSum; // Cardboard
  62. public:
  63.   void visit(Aluminum* al) {
  64.     alSum += al->weight();
  65.     out << "weight of Aluminum = "
  66.         << al->weight() << endl;
  67.   }
  68.   void visit(Paper* p) {
  69.     pSum += p->weight();
  70.     out << "weight of Paper = " 
  71.       << p->weight() << endl;
  72.   }
  73.   void visit(Glass* g) {
  74.     gSum += g->weight();
  75.     out << "weight of Glass = "
  76.         << g->weight() << endl;
  77.   }
  78.   void visit(Cardboard* c) {
  79.     cSum += c->weight();
  80.     out << "weight of Cardboard = "
  81.         << c->weight() << endl;
  82.   }
  83.   void total(ostream& os) {
  84.     os << "Total weight Aluminum:"
  85.        << alSum << endl;
  86.     os << "Total weight Paper:"
  87.        << pSum << endl;
  88.     os << "Total weight Glass:"
  89.        << gSum << endl;
  90.     os << "Total weight Cardboard:" 
  91.        << cSum << endl;
  92.   }
  93. };
  94.  
  95. int main() {
  96.   vector<Trash*> bin;
  97.   // fillBin() still works, without changes, but
  98.   // different objects are prototyped:
  99.   fillBin("Trash.dat", bin);
  100.   // You could even iterate through
  101.   // a list of visitors!
  102.   PriceVisitor pv;
  103.   WeightVisitor wv;
  104.   vector<Trash*>::iterator it = bin.begin();
  105.   while(it != bin.end()) {
  106.     (*it)->accept(pv);
  107.     (*it)->accept(wv);
  108.     it++;
  109.   }
  110.   pv.total(out);
  111.   wv.total(out);
  112.   purge(bin);
  113. } ///:~
  114.